package logic

import (
	"context"
	"userrpc/userrpcclient"

	"userapi/internal/svc"
	"userapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserRegisterLogic) UserRegister(req *types.RegRequest) (resp *types.RegResponse, err error) {
	// todo: add your logic here and delete this line
	username := req.Username
	password := req.Password
	mobile := req.Mobile
	code := req.Code
	res, err := l.svcCtx.UserRpc.Register(context.Background(), &userrpcclient.RegRequest{
		Username: username,
		Password: password,
		Code:     code,
		Mobile:   mobile,
	})
	if err != nil {
		return &types.RegResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
		}, nil
	}
	if res.Code != 200 {
		return &types.RegResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
		}, nil
	}
	return &types.RegResponse{
		Code: 200,
		Msg:  "注册成功",
	}, nil
}
